<template>
  <BasicDrawer
    @register="register"
    @ok="handleSubmit"
    width="60%"
    :showFooter="true"
    :okAuth="'m8:machine:over:edit'"
  >
    <template #title>
      <span>
        完工数量登记- 【
        <!-- <DictLabel
          v-if="flag"
          :dictType="'m8_task_over_type'"
          :dictValue="record.overType"
          :defaultValue="record.overType"
        /> -->
        {{ record.picname }}
        】机床:【{{ record.machineId }}】
        <DictLabel v-if="flag" :dictType="'m8_task_over_type'" :dictValue="record.overType" />
      </span>
    </template>
    <BasicForm @register="registerForm" />
    <div
      style="color: red; font-size: 20px; text-align: center"
      v-if="record.overType == '0' && record.machineStatus != '1'"
    >
      当前任务未做调试，请将调试信息补充完整！
    </div>

    <BasicModal
      @register="register2"
      v-bind="$attrs"
      :canFullscreen="false"
      cancelText="取消操作"
      :showOkBtn="false"
      width="40%"
    >
      <!-- @ok="handSave"  okText="是"-->
      <div style="height: 300px; display: flex; justify-content: center; align-items: center">
        <div>
          <div style="text-align: center; font-size: 50px; color: red">
            机床【{{ record.machineId }}
            】-【
            <DictLabel :dictType="'m8_task_over_type'" :dictValue="record.overType" />
            】
          </div>
          <div style="text-align: center; font-size: 40px"> 图纸【{{ record.picname }}】 </div>
        </div>
      </div>
      <template #appendFooter>
        <a-button @click="handSave" type="primary" :loading="iconLoading" :disabled="btnDisabled">
          <!-- <Icon :icon="ant-design:check-outlined" /> -->
          <Icon icon="ant-design:check-outlined" />
          继续执行
        </a-button>
      </template>
    </BasicModal>
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'WorkCalendarModal',
  });
</script>
<script setup lang="ts">
  import { defineComponent, ref, computed } from 'vue';
  import { BasicModal, useModalInner, useModal } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { officeTreeData } from '/@/api/sys/office';
  import { overForm } from '/@/api/m8/flow/flow';
  import { taskOversave } from '/@/api/m8/machine/machine';
  import { DictLabel } from '/@/components/Dict';
  import { Icon } from '/@/components/Icon';
  import { save } from '/@/api/m8/order/order';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  const emit = defineEmits(['success', 'register']);
  const { showMessage } = useMessage();
  let iconLoading = ref<any>(true);
  let btnDisabled = ref<boolean>(false);
  let num = ref<any>(3);

  // import { DatePicker } from 'ant-design-vue';
  let record = ref<any>({
    id: '',
    taskCode: '',
    machineId: '',
    operCode: '',
    overType: '',
    picno: '',
    ope: '',
    picname: '',
    machineStatus: '',
  });
  let flag = ref(false);
  const [register2, { openModal, setModalProps: setModalProps2, closeModal: closeModal2 }] =
    useModal();

  // let obj= ref<any>()
  const [register, { closeDrawer, setDrawerProps }] = useDrawerInner(async (e) => {
    flag.value = false;
    await resetFields();
    console.log(e);
    record.value = e;
    flag.value = true;

    let inputFormSchemas: FormSchema[] = [
      {
        label: '完工数',
        field: 'iqty',
        component: 'Input',
        componentProps: {
          maxlength: 10,
        },
        required: true,
      },
      {
        label: '负责人',
        field: 'manCodes',
        fieldLabel: 'manNames',
        component: 'TreeSelect',
        required: true,
        componentProps: {
          api: officeTreeData,
          treeCheckable: true,
          params: { isLoadUser: true, userIdPrefix: '', isAll: true, isShowCode: true },
          canSelectParent: true,
          allowClear: true,
        },
      },
      {
        label: '切换后机床',
        field: 'toMachine',
        component: 'Input',
        componentProps: {
          maxlength: 10,
        },
        ifShow: record.value.overType == '3',
        required: true,
      },
      {
        label: '完工备注',
        field: 'remarks',
        component: 'InputTextArea',
        // required: true,
        componentProps: {
          maxlength: 500,
          rows: 5,
        },
        // colProps: { lg: 24, md: 24 },
      },

      {
        label: '调试负责人',
        field: 'dmanCodes',
        fieldLabel: 'dmanNames',
        component: 'TreeSelect',
        required: true,
        componentProps: {
          api: officeTreeData,
          treeCheckable: true,
          params: { isLoadUser: true, userIdPrefix: '', isAll: true, isShowCode: true },
          canSelectParent: true,
          allowClear: true,
        },
        ifShow: record.value.overType == '0' && record.value.machineStatus != '1',
      },
      {
        label: '实际加工时长',
        field: 'dsjjgtime',
        component: 'Input',
        componentProps: {
          maxlength: 10,
        },
        required: true,
        ifShow: record.value.overType == '0' && record.value.machineStatus != '1',
      },
    ];
    setProps({
      labelWidth: 120,
      schemas: inputFormSchemas,
      baseColProps: { lg: 24, md: 24 },
    });

    // if(getTitle.value.id){
    //   setModalProps({ loading: true });
    //   await resetFields();
    //   const res = await overForm({ id: getTitle.value.id });
    //   getTitle.value = res.m8OrderPicFlowOver || {};
    //   getTitle.value.__t = new Date().getTime();
    //   setFieldsValue(getTitle.value);
    //   setModalProps({ loading: false });
    // }
  });

  async function handleSubmit() {
    iconLoading.value = true;
    btnDisabled.value = false;
    await validate();
    openModal(true, '');
    setTimeout(() => {
      iconLoading.value = false;
    }, 3000);
  }
  // async function handleOk() {
  //   handSave('1');
  // }
  // async function closeFunc() {
  //   handSave('0');
  // }

  async function handSave() {
    try {
      btnDisabled.value = true;
      const data = await validate();
      setModalProps2({ confirmLoading: true });
      console.log(record.value);

      const params: any = {
        id: '',
        taskCode: record.value.taskCode,
        machineId: record.value.machineId,
        operCode: record.value.operCode,
        overType: record.value.overType,
        ope: record.value.ope,
        ...data,
      };
      console.log('submit', params, data, record);
      setTimeout(closeModal2);
      const res = await taskOversave(params);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', '');
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage('您填写的信息有误，请根据提示修正。');
      }
      console.log('error', error);
      btnDisabled.value = true;
    } finally {
      btnDisabled.value = true;
      setModalProps2({ confirmLoading: false });
    }
  }

  // let inputFormSchemas: FormSchema[] = [];
  const [registerForm, { resetFields, setFieldsValue, validate, setProps }] = useForm();
</script>
<style scoped>
  .ant-picker {
    width: 80%;
  }
</style>
